package com.cxz.kotlin.baselibs.base

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.os.Parcelable
import com.cxz.kotlin.baselibs.ext.showToast
import com.cxz.kotlin.baselibs.interfaces.OnCountDownCallBack
import com.cxz.kotlin.baselibs.mvp.IPresenter
import com.cxz.kotlin.baselibs.mvp.IView
import com.cxz.kotlin.baselibs.utils.LogUtil
import com.cxz.kotlin.baselibs.widget.CustomLoadingDialog
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
import io.reactivex.rxjava3.core.Observable
import io.reactivex.rxjava3.disposables.Disposable
import io.reactivex.rxjava3.functions.Action
import io.reactivex.rxjava3.functions.Consumer
import org.jetbrains.anko.startActivityForResult
import java.io.Serializable
import java.lang.ref.WeakReference
import java.util.concurrent.TimeUnit

/**
 * @author chenxz
 * @date 2018/11/19
 * @desc BaseMvpActivity
 */
abstract class BaseMvpActivity<in V : IView, P : IPresenter<V>> : BaseActivity(), IView {

    /**
     * Presenter
     */
    protected var mPresenter: P? = null

    protected var timecountDis: Disposable? = null

    protected abstract fun createPresenter(): P

    var needShowLoading = true

    protected val mLoadingDialog:CustomLoadingDialog by lazy {
        CustomLoadingDialog(WeakReference(this))
    }

    override fun initView() {
        mPresenter = createPresenter()
        mPresenter?.attachView(this as V)
        setStatusBarIcon(true)
    }

    override fun onDestroy() {
        super.onDestroy()
        mLoadingDialog.dismiss()
        disposeTimeCount()
        mPresenter?.detachView()
        this.mPresenter = null
    }

    fun disposeTimeCount() {
        if (null != timecountDis && !timecountDis!!.isDisposed) {
            timecountDis!!.dispose()
            timecountDis = null
        }
    }

    override fun showLoading() {
        if (needShowLoading) {
            try {
                if (!mLoadingDialog.isShowing) {
                    mLoadingDialog.show()
                }
            } catch (e: java.lang.Exception) {
                LogUtil.e("showLoading","${e.toString()},${e.message}")
            }
        }
    }

    override fun hideLoading() {
        mLoadingDialog.hide()
    }

    override fun showError(errorMsg: String) {
        showToast(errorMsg)
    }

    override fun showDefaultMsg(msg: String) {
        showToast(msg)
    }

    override fun showMsg(msg: String) {
        showToast(msg)
    }

    @SuppressLint("AutoDispose")
    open fun countDown(
        countDownCallBack: OnCountDownCallBack,
        intervalTime: Long = 1000,
        seconds: Long = Long.MAX_VALUE
    ) {
        try {
            timecountDis?.let {
                if (!it.isDisposed()) {
                    it.dispose()
                    timecountDis = null
                }
            }
            if (null == timecountDis) {

                timecountDis =
                    Observable.interval(intervalTime, TimeUnit.MILLISECONDS).take(seconds)
                        .subscribeOn(AndroidSchedulers.mainThread())
                        .subscribe(object : Consumer<Long> {
                            override fun accept(t: Long?) {
                                countDownCallBack.countDowning(t)
                            }

                        }, object : Consumer<Throwable> {
                            override fun accept(t: Throwable?) {

                            }


                        }, object : Action {
                            override fun run() {
                                countDownCallBack.countDownEnd()
                                disposeTimeCount()
                            }

                        })
            }
        } catch (e: Exception) {
            LogUtil.e("countDown", e.message)
        }
    }





}

